package com.example.itshome.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.impl.ViewOutlineProviderImpl;

import java.io.File;
import java.util.List;

public class CreatePostRecycleViewAdapter extends RecyclerView.Adapter<CreatePostRecycleViewAdapter.CreatePostViewHolder> {

    private final List<File> mData;
    private final Context context;

    public CreatePostRecycleViewAdapter(Context context,List<File> list){
        mData=list;
        this.context=context;
    }

    @NonNull
    @Override
    public CreatePostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=View.inflate(context, R.layout.recycle_creare_post_item,null);
        view.setOutlineProvider(new ViewOutlineProviderImpl(20));
        view.setClipToOutline(true);
        CreatePostViewHolder holder=new CreatePostViewHolder(view);
        return holder;
    }

    private void readPicture(){
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        //参数一:对应的数据的URI 参数二:使用该函数表示要查找文件的MIME类型
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        ((AppCompatActivity)context).startActivityForResult(intent, 1);
    }

    @Override
    public void onBindViewHolder(@NonNull CreatePostViewHolder holder, int position) {
        if(position<getItemCount()-1){
            Glide.with(context).load(mData.get(position)).placeholder(R.drawable.sample_picture).into(holder.imageView);
        }else {
            Glide.with(context).load(R.drawable.create_post_page_add_im).placeholder(R.drawable.sample_picture).into(holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                    } else {
                        //访问相册
                        readPicture();
                    }

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size()+1;
    }

    static class CreatePostViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        public CreatePostViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imv_recycle_create_post_image);

            imageView.setOutlineProvider(new ViewOutlineProviderImpl(20));
            imageView.setClipToOutline(true);
        }
    }
}
